package com.jianying.day07;

import com.jianying.beans.WaterSensor;
import com.jianying.func.WaterSensorMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * ClassName: Flink03_ProcessFunction
 * Package: com.atguigu.day07
 * Description:
 *
 * @Author: tubage
 * @Create: 2024/4/8 11:27
 * @Version: 1.0
 */
public class Flink03_ProcessFunction {
    public static void main(String[] args) {
        // TODO 处理函数分类
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> wsDS = env.socketTextStream("localhost", 8888)
                .map(new WaterSensorMapFunction());

        wsDS.process(
                new ProcessFunction<WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                    }
                }
        );
        wsDS.keyBy(waterSensor -> waterSensor.getId())
                .process(
                        new KeyedProcessFunction<String, WaterSensor, String>() {
                            @Override
                            public void processElement(WaterSensor value, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }
                        }
                );

        wsDS.keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .process(
                        new ProcessWindowFunction<WaterSensor, WaterSensor, String, TimeWindow>() {
                            @Override
                            public void process(String s, ProcessWindowFunction<WaterSensor, WaterSensor, String, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<WaterSensor> out) throws Exception {

                            }
                        }
                );
        wsDS.windowAll(TumblingEventTimeWindows.of(Time.seconds(10)))
                .process(
                        new ProcessAllWindowFunction<WaterSensor, WaterSensor, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<WaterSensor, WaterSensor, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<WaterSensor> out) throws Exception {

                            }
                        }
                );

        wsDS.connect(wsDS)
                .process(
                        new CoProcessFunction<WaterSensor, WaterSensor, String>() {
                            @Override
                            public void processElement1(WaterSensor value, CoProcessFunction<WaterSensor, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }

                            @Override
                            public void processElement2(WaterSensor value, CoProcessFunction<WaterSensor, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }
                        }
                );

        wsDS.keyBy(wsDS.getId())
                .intervalJoin(wsDS.keyBy(wsDS.getId()))
                .between(Time.seconds(-5), Time.seconds(5))
                .process(
                        new ProcessJoinFunction<WaterSensor, WaterSensor, Tuple2<WaterSensor,WaterSensor>>() {
                            @Override
                            public void processElement(WaterSensor left, WaterSensor right, ProcessJoinFunction<WaterSensor, WaterSensor, Tuple2<WaterSensor, WaterSensor>>.Context ctx, Collector<Tuple2<WaterSensor, WaterSensor>> out) throws Exception {

                            }
                        }
                );

    }
}
